package com.atguigu.lease.admin.controller.apartment;

import com.atguigu.lease.admin.service.AttrKeyService;
import com.atguigu.lease.admin.service.AttrValueService;
import com.atguigu.lease.model.bean.AttrKey;
import com.atguigu.lease.model.bean.AttrValue;
import com.atguigu.lease.model.result.Response;
import com.atguigu.lease.model.vo.AttrKeyVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "房间属性管理模块")
@RestController
@RequestMapping("/admin/attr")
public class AttrController {

    @Autowired
    AttrKeyService attrKeyService;
    @Autowired
    AttrValueService attrValueService;
//    /admin/attr/key/deleteById?attrKeyId=1
    @Operation(summary = "5、根据id删除属性名")
    @DeleteMapping("/key/deleteById")
    public Response deleteAttrKeyById(@RequestParam("attrKeyId") Long id){
        long count = attrValueService.count(Wrappers.lambdaQuery(AttrValue.class)
                .eq(AttrValue::getAttrKeyId, id));
        if(count > 0){
            throw new RuntimeException("属性下存在未删除的值");
        }
        attrKeyService.removeById(id);
        return Response.ok(null);
    }
//    /admin/attr/value/deleteById
    @Operation(summary = "4、根据id删除属性值")
    @DeleteMapping("/value/deleteById")
    public Response deleteAttrValueById(@RequestParam Long id){
        attrValueService.removeById(id);
        return Response.ok(null);
    }

//    /admin/attr/key/saveOrUpdate
    @Operation(summary = "3、新增或更新属性名")
    @PostMapping("key/saveOrUpdate")
    public Response saveOrUpdateKey(@RequestBody AttrKey attrKey) {
        attrKeyService.saveOrUpdate(attrKey);
        return Response.ok(null);
    }

    ///admin/attr/value/saveOrUpdate
    @Operation(summary = "2、新增更新属性值")
    @PostMapping("value/saveOrUpdate")
    public Response saveOrUpdateValue(@RequestBody AttrValue attrValue) {
        attrValueService.saveOrUpdate(attrValue);
        return Response.ok(null);
    }
    @Operation(summary = "1、查询房间属性和属性值列表")
    @GetMapping("list")
    public Response<List<AttrKeyVo>> list(){
        List<AttrKeyVo> list = attrKeyService.listAttrKeyVo();
        return Response.ok(list);
    }




}
